var year_month="月销售报总表";
var $table = $("#list_table");

var queryDate = "";

var consultDay = {
    elem: "#consultDay",
    max: 0,
    type: 'month',
    /* Object
     date: 22
     hours: 0
     minutes: 0
     month: 9
     seconds: 0
     year: 2017*/
    done: function (value, date) {
        queryDate = value;
        //选完基准日期后查询
        $table.bootstrapTable("refresh");
    }
};
laydate.render(consultDay);

$(function () {
    var newDate = new Date();
    var year = newDate.getFullYear();
    var month=newDate.getMonth();
    year_month=year+"年"+(month+1)+year_month;
    var url = urlcore + "/api/statistics/getMonthSaleReport";
    initTable($table, url);
    $("#exportRange").change(function () {
        $table.bootstrapTable('refreshOptions', {
            exportDataType: $(this).val()
        });
    });
});


var columns = [{
    title: "序号",
    align: "center",
    width: "50px",
    formatter: function (value, row, index) {
        return (index + 1);
    }
}, {
    field: "goodsName",
    title: "产品名称",
    halign: "center",
    align: "left",
    width: "400px",
}, {
    field: "saleCount",
    title: "销售数量",
    halign: "center",
    valign: "middle",
    align: "center",
    width: "150px",
}, {
    field: "price",
    title: "商品标价",
    halign: "center",
    valign: "middle",
    align: "center",
    width: "160px",
}, {
    field: "orderCount",
    title: "消费订单数",
    halign: "center",
    valign: "middle",
    align: "center",
    width: "120px",
}, {
    field: "male",
    title: "男用户",
    halign: "center",
    valign: "middle",
    align: "center",
    width: "120px",
}, {
    field: "female",
    title: "女用户",
    halign: "center",
    valign: "middle",
    align: "center",
    width: "120px",
}, {
    field: "ltTwenty",
    title: "20岁以下",
    halign: "center",
    valign: "middle",
    align: "center",
    width: "120px",
}, {
    field: "twenty2thirty",
    title: "20-30岁",
    halign: "center",
    align: "center",
    valign: "middle",
    width: "120px",
}, {
    field: "thirty2forty",
    title: "31-40岁",
    halign: "center",
    align: "center",
    valign: "middle",
    width: "120px",
}, {
    field: "forty2fifty",
    title: "41-50岁",
    halign: "center",
    align: "center",
    valign: "middle",
    width: "120px",
}, {
    field: "gtFifty",
    title: "50岁以上",
    halign: "center",
    align: "center",
    valign: "middle",
    width: "120px",
}];

//查询参数
var queryParams = function (params) {
    console.log("params:", params);
    var param = params;
    param.queryDate=queryDate;
    return param;
};

//初始化表格，有已有和待选的商品两类表格 tableins:表格实例 ,
// insTag 0表示当前已添加表格 1表示待添加的表格
function initTable(tableins, url) {
    tableins.bootstrapTable({
        columns: columns,
        method: "get",
        url: url,
        dataType: "json",
        queryParams: function (params) {
            return queryParams(params);
        },
        //返回数据预处理
        responseHandler: function (cbdata) {
            console.log(cbdata);
            var res = cbdata.data;
            return res;
        },
        cache: false,
        // toolbar: '#toolbar',//自定义工具栏，不同表格工具栏不同，一个jQuery 选择器，指明自定义的toolbar 例如:#toolbar, .toolbar.
        pagination: true,//启用分页条
        sidePagination: "client",//分页方式client或server
        pageSize: 10,//每页大小
        pageList: [10, 20, 40, 80],//可以选择每页大小
        showRefresh: true,//显示刷新
        sortable: true,//是否启用排序
        clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,//搜索,未设置分页方式为"server"时只是客户端搜索
        showExport: true,
        exportTypes: ['excel'],
        exportDataType:"all",
        exportOptions:{
            fileName:year_month
        },
        buttonsAlign:"left",//工具条位置，默认righ
        searchAlign:"left",//搜索框位置，默认right
        toolbarAlign:"right",//toolbar位置，默认left
    });
}

